package com.liu.communitysystemend.module.ParkingSpace.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.ParkingRecord.domain.ParkingRecord;
import com.liu.communitysystemend.module.ParkingRecord.service.ParkingRecordService;
import com.liu.communitysystemend.module.ParkingSpace.domain.ParkingSpace;
import com.liu.communitysystemend.module.ParkingSpace.domain.QPackingSpaceDto;
import com.liu.communitysystemend.module.ParkingSpace.mapper.ParkingSpaceMapper;
import com.liu.communitysystemend.module.ParkingSpace.service.ParkingSpaceService;
import com.liu.communitysystemend.module.license.domain.License;
import com.liu.communitysystemend.module.license.service.LicenseService;
import com.liu.communitysystemend.module.user.domain.User;
import com.liu.communitysystemend.module.user.service.UserService;
import com.liu.communitysystemend.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class ParkingSpaceServiceImpl implements ParkingSpaceService {
    @Autowired
    private ParkingSpaceMapper parkingSpaceMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private LicenseService licenseService;

    @Autowired
    private ParkingRecordService parkingRecordService;

    @Override
    public ParkingSpace getVisitorById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<ParkingSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkingSpace::getId,id);
        ParkingSpace parkingSpace = parkingSpaceMapper.selectOne(wrapper);
        return parkingSpace;
    }
    public ParkingSpace selectOneById(String id){
        ParkingSpace parkingSpace=parkingSpaceMapper.selectById(id);
        return  parkingSpace;
    }

    @Override
    public ParkingSpace update(ParkingSpace parkingSpace) {
        ParkingSpace parkingSpace1 = selectOneById(parkingSpace.getId());
        if (parkingSpace1.getStatus()==1){
            throw new CommonException("变更失败，此车位不为空");
        }
        LambdaQueryWrapper<ParkingSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkingSpace::getId,parkingSpace.getId());

        int update = parkingSpaceMapper.update(parkingSpace, wrapper);
        if (update>0){
            return parkingSpace;
        }
        throw new CommonException(502,"更新失败");
    }

    @Override
    public ParkingSpace add(ParkingSpace parkingSpace) {
        if(parkingSpace ==null){
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert=parkingSpaceMapper.insert(parkingSpace);
        if(insert>0){
            return parkingSpace;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Page<ParkingSpace> getPage(ParkingSpace parkingSpace) {
        LambdaQueryWrapper<ParkingSpace> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ParkingSpace::getIsDeleted,0);
        Page<ParkingSpace> page=new Page<>(parkingSpace.getPage().getPage(),parkingSpace.getPage().getSize());
        Page<ParkingSpace> poiPage=(Page<ParkingSpace>) parkingSpaceMapper.selectPage(page,wrapper);
        return poiPage;
    }

    @Override
    public Integer deleteById(String id) {
        ParkingSpace parkingSpace = selectOneById(id);
        if(parkingSpace==null){
            throw new CommonException(4004,"找不到id为:"+id+"的数据");
        }
        int i=parkingSpaceMapper.deleteById(id);
        return i;
    }

    @Override
    public Integer test() {
        for (int i = 0; i < 20; i++) {
            ParkingSpace parkingSpace = new ParkingSpace();
            parkingSpace.setId("100"+i);
            parkingSpace.setName("P"+i);
            parkingSpace.setType(i%3);
            parkingSpace.setStatus(i%2);
            parkingSpace.setUserId("100"+i);
            parkingSpaceMapper.insert(parkingSpace);
        }
        return 1;
    }

    @Override
    public List<ParkingSpace> getList(ParkingSpace parkingSpace) {
        return  parkingSpaceMapper.getList(parkingSpace);
    }

    @Override
    public User CheckInfo(String id) {
        if (id==null || id==""){
            throw new CommonException(501,"车位id不能为空");
        }
        User user = userService.selectByRoomId(id);
        if (user!=null){
            return user;
        }
        throw new CommonException(501,"该车位为空");
    }

    @Override
    public Result checkIn(QPackingSpaceDto qPackingSpaceDto) {
        User user = userService.selectByIdCard(qPackingSpaceDto.getCertNo());
        if (user==null){
            throw new CommonException("证件号输入错误,或未注册");
        }
        License license = licenseService.selectByName(qPackingSpaceDto.getCarNo());
        if (license==null){
            throw new CommonException("车牌号有误，或未添加至数据库");
        }
        ParkingSpace parkingSpace = getVisitorById(qPackingSpaceDto.getParkingSpaceId());
        if (parkingSpace==null){
            throw new CommonException("车位id不能为空 或传入的车位id有误");
        }

        if (qPackingSpaceDto.getType()==0){
            throw new CommonException(500,"临时车位不能登记");
        } else if (qPackingSpaceDto.getType()==1){
            Date date = new Date();
            ParkingRecord parkingRecord = new ParkingRecord();
            parkingRecord.setPayStatus(0);
            parkingRecord.setEntryTime(date);
            parkingRecord.setFee(150000.00);
            parkingRecord.setParkingSpaceId(qPackingSpaceDto.getParkingSpaceId());
            parkingRecord.setLicense(license.getId());
            parkingRecord.setUserId(user.getId());
            parkingRecord.setDepartureTime(new Date(date.getTime() + (3600 * 1000 * 24 * 365 * 70)));
            parkingRecordService.add(parkingRecord);

            parkingSpace.setUserId(user.getId());
            parkingRecord.setPayStatus(1);
            update(parkingSpace);
        }else if (qPackingSpaceDto.getType()==2){
            Date date = new Date();
            ParkingRecord parkingRecord = new ParkingRecord();
            parkingRecord.setPayStatus(0);
            parkingRecord.setLicense(license.getId());
            parkingRecord.setUserId(user.getId());
            parkingRecord.setEntryTime(new Date());
            parkingRecord.setFee(qPackingSpaceDto.getDays()*10.0);
            parkingRecord.setDepartureTime(new Date(date.getTime()+((3600 * 1000 * 24 * qPackingSpaceDto.getDays()))));
            parkingRecord.setParkingSpaceId(parkingSpace.getId());

            parkingRecordService.add(parkingRecord);
            parkingSpace.setUserId(user.getId());
            parkingRecord.setPayStatus(1);
            update(parkingSpace);
        }
      throw new CommonException(500,"请传入正确的车位类型");
    }

    @Override
    public Result checkOut(QPackingSpaceDto qPackingSpaceDto) {
        User user = userService.selectByIdCard(qPackingSpaceDto.getCertNo());
        if (user==null){
            throw new CommonException("证件号输入错误,或未注册");
        }
        License license = licenseService.selectByName(qPackingSpaceDto.getCarNo());
        if (license==null){
            throw new CommonException("车牌号有误，或未添加至数据库");
        }
        ParkingSpace parkingSpace = getVisitorById(qPackingSpaceDto.getParkingSpaceId());
        if (parkingSpace==null){
            throw new CommonException("车位id不能为空 或传入的车位id有误");
        }
        parkingSpace.setUserId("");
        update(parkingSpace);
        ParkingRecord parkingRecord = new ParkingRecord();
        parkingRecord.setParkingSpaceId(parkingSpace.getId());
        parkingRecord.setUserId(user.getId());
        ParkingRecord parkingRecord1 = parkingRecordService.selectbyValid(parkingRecord);
        if (parkingRecord1==null){
            throw new CommonException(5000,"退停车位异常,查询停车记录为空");
        }
        parkingRecord1.setDepartureTime(new Date());
        parkingRecord1.setValid(0);
        return Result.success(parkingRecordService.update(parkingRecord1));
    }

}

